#将函数应用于矩阵和数据框
a <- 5
sqrt(a)
b<-c(1.243,5.654,2.99)
round(b,3)#四舍五入，保留位数

c <- matrix(runif(12),nrow = 3)
?runif#平均分布
log(c)
mean(c)
apply(c,1,FUN = 'sum')
#mean(...,trim=0.2) 除去前后的各自20%

#step1
options(digits = 2)#限定输出小数位数  
student <- c('John Davis','Angela Williams','Bullwinkle Moose','David Jones','Janice Markhammer','Cheryl Cushing','Reuven Ytzrhak','Greg Knox','Joel England','Mary Rayburn')
math <- c(502,600,412,358,495,512,410,625,573,522)
science <- c(95,99,80,82,75,85,80,95,89,86)
english <- c(25,22,18,15,20,28,15,30,27,18)
roster <- data.frame(student,math,science,english,stringsAsFactors = FALSE)
#step2
?scale
z <- scale(roster[,2:4])#中心化和标准化，得出偏离指数
#step3
score <- apply(z,1,mean)#求各个学生偏离指数的均值
roster <- cbind(roster,score)#
#step4
y <- quantile(score,c(.8,.6,.4,.2))
#step5 
roster$grade[score >= y[1]] <- 'A'
roster$grade[score < y[1] & score >= y[2]] <- 'B'
roster$grade[score < y[2] & score >= y[3]] <- 'C'
roster$grade[score < y[3] & score >= y[4]] <- 'D'
roster$grade[score < y[4]] <- 'F'
#step6 
name <- strsplit((roster$student),' ')
firstname <- sapply(name,'[',1)
lastname <- sapply(name,'[',2)
roster <- cbind(firstname,lastname,roster[,-1])

roster <- roster[order(lastname,firstname),]

#数据处理结束，学习控制流和函数 
file.edit('process.r') 
?file.edit
